package cn.unknowpupil.init.controller.monitor;


import cn.unknowpupil.init.common.enums.BusinessType;
import cn.unknowpupil.init.common.resopnse.ResultResponse;
import cn.unknowpupil.init.components.annotation.Log;
import cn.unknowpupil.init.domain.dto.LoginUser;
import cn.unknowpupil.init.domain.system.SysLoginInfo;
import cn.unknowpupil.init.domain.system.SysUser;
import cn.unknowpupil.init.service.system.ISysLoginInfoService;
import cn.unknowpupil.init.service.system.ISysUserService;
import cn.unknowpupil.init.utils.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created at 2020-10-20 by  w10g <br>
 * Initialize SysLoginInfo  ... <br>
 * 系统访问记录 视图浏览层处理
 *
 * @author unknowpupil
 * @since 0.0.1
 */
@Api(tags = {"系统-系统访问记录模块"}, value = "SysLoginInfo")
@RestController
@RequestMapping(value = "admin/sys-login-info")
@RequiredArgsConstructor
public class SysLoginInfoController {

    private final ISysLoginInfoService sysLoginInfoService;
    private final ISysUserService userService;


    /**
     * 删除
     *
     * @param ids {@code Long}
     * @return {@link ResultResponse}
     */
    @Log(title = "remove", businessType = BusinessType.DELETE)
    @DeleteMapping("remove/{ids}")
    public ResultResponse remove(@PathVariable("ids") Long[] ids) {
        // 业务逻辑
        boolean deleted = sysLoginInfoService.delete(ids);
        if (deleted) {
            return ResultResponse.success("删除成功");
        } else {
            return ResultResponse.buildFailure("删除失败");
        }
    }

    @Log(title = "清空登录日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public ResultResponse clean() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        boolean remove = false;
        if (SecurityUtils.isSuperAdmin(loginUser.getPid())) {

            remove = sysLoginInfoService.remove(null);
        } else if (SecurityUtils.isAdmin(loginUser.getPid())) {
            List<String> sunUser = userService.getAllSunUserByUserId(loginUser.getId());
                 sunUser.add(loginUser.getUsername());
                LambdaQueryWrapper<SysLoginInfo> query = new LambdaQueryWrapper<>();
                query.in(SysLoginInfo::getUserName, sunUser);
                remove = sysLoginInfoService.remove(query);

        } else {
            return ResultResponse.buildFailure("您没有权限");
        }

        if (remove) {
            return ResultResponse.success("清除成功");
        } else {
            return ResultResponse.buildFailure("清除失败");
        }
    }

    /**
     * 分页
     *
     * @param current {@code int} 页码
     * @param size    {@code int} 笔数
     * @return {@link ResultResponse}
     */
    @GetMapping("page/{current}/{size}")
    public ResultResponse<IPage<SysLoginInfo>> page(@PathVariable("current") int current, @PathVariable("size") int size, @ModelAttribute SysLoginInfo SysLoginInfo) {
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        if (!SecurityUtils.isSuperAdmin(loginUser.getPid())) {
//            SysLoginInfo.setUserName(loginUser.getUsername());
//        }

        IPage<SysLoginInfo> page = sysLoginInfoService.page(current, size, SysLoginInfo);
        return ResultResponse.buildSuccess(page);
    }
}
